public class Demo {
    //使用众数和非众数来找出找出数组一半长度的的元素
    public static void main(String[] args) {
        int[] elem = {1,2,1,1};
        int result = elem[0];
        int time = 1;
        for (int i = 1; i < elem.length; i++) {
            if(time != 0){
                if(result != elem[i]){
                    time--;
                }else {
                    time++;
                }
            }else {
                result = elem[i];
                time++;
            }
        }
        int len = elem.length/2;
        int count = 0;
        for (int i = 0; i < elem.length; i++) {
            if(elem[i] == result){
                count++;
            }
        }
        if(count > len){
            System.out.println(result);
        }
    }

}
